{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 打印所有单行变量\n",
    "from IPython.core.interactiveshell import InteractiveShell\n",
    "InteractiveShell.ast_node_interactivity = 'all'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 文件\n",
    "### 打开文件\n",
    "```\n",
    "open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opene r=None)\n",
    "```\n",
    "\n",
    "| 模式 | 描述 |\n",
    "|------|------|\n",
    "| r | 以读方式打开文件，可读取文件信息,默认模式|\n",
    "| w | 以写方式打开文件，可向文件写入信息。如文件存在，则清空该文件，再写入新内容 |\n",
    "| x | 以写模式新建一个文件 |\n",
    "| a | 以追加模式打开文件（文件指针自动移到文件末尾），如果文件不存在则创建 |\n",
    "| + | 配合其他模式r,w,a等,以读和写模式 |\n",
    "| b | 二进制模式 |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['buffer',\n",
       " 'close',\n",
       " 'closed',\n",
       " 'detach',\n",
       " 'encoding',\n",
       " 'errors',\n",
       " 'fileno',\n",
       " 'flush',\n",
       " 'isatty',\n",
       " 'line_buffering',\n",
       " 'mode',\n",
       " 'name',\n",
       " 'newlines',\n",
       " 'read',\n",
       " 'readable',\n",
       " 'readline',\n",
       " 'readlines',\n",
       " 'seek',\n",
       " 'seekable',\n",
       " 'tell',\n",
       " 'truncate',\n",
       " 'writable',\n",
       " 'write',\n",
       " 'writelines']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f = open(\"test.txt\", \"w\")  # 在当前文件夹打开已存在的文件，文件不存在则报错\n",
    "[i for i in dir(f) if not i.startswith('_')]  # --> 文件常用方法\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 在对文件进行写入操作之后，一定要牢记一个事情：`file.close()`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### with...as..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "29"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "['\\n', \"This is about 'with...as...'\"]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用with语句不用关闭文件\n",
    "with open(\"test.txt\", \"w\") as f:\n",
    "    f.write(\"\\nThis is about 'with...as...'\")  # --> 返回字符长度\n",
    "\n",
    "with open(\"test.txt\") as f:\n",
    "    f.readlines()  # -->"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### 常用文件方法\n",
    "\n",
    "#### f.read([size])\n",
    "\n",
    "如果指定了参数size，就按照该指定长度从文件中读取内容，否则，就读取全文直到EOF\n",
    "\n",
    "#### f.readline([size])\n",
    "\n",
    "* 如果指定了参数size,则读取指定长度的内容,直到行尾才会读取下一行\n",
    "* 如果不指定参数,则一行一行读取\n",
    "\n",
    "#### f.readlines([hint])\n",
    "* 返回该文件中包含的所有行。\n",
    "* 如果设置可选参数hint, 则读取指定长度的字节, 并且将这些字节按行分割\n",
    "\n",
    "> EOF(End-of-file)\n",
    "\n",
    "#### f.write(string)\n",
    "将 string 写入到文件中, 然后`返回`写入的字符数\n",
    "\n",
    "#### f.tell()\n",
    "返回文件对象当前所处的位置, 它是从文件开头开始算起的字节数\n",
    "\n",
    "#### f.seek(offset[, whence])\n",
    "这个函数的功能就是让指针移动。\n",
    "whence的值：\n",
    "- 默认值是0，表示从文件开头开始计算指针偏移的量（简称偏移量）。此时offset必须是大于等于0的整数。\n",
    "- 是1时，表示从当前位置开始计算偏移量。offset如果是负数，表示从当前位置向前移动，整数表示向后移动。\n",
    "- 是2时，表示相对文件末尾移动。"
   ]
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4rc1"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": "block",
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": "15",
    "lenType": "10",
    "lenVar": "60"
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
